package com.health.viewscontrols.focusgroup;

import java.util.ArrayList;

import com.health.viewscontrols.NovaFragmentActivity;
import com.health.viewscontrols.PersonalFileActivity.onFragmentSave;
import com.health.viewscontrols.PersonalFileActivity.onFragmentUpData;
import com.health.R;
import com.health.adapter.FragmentAdapter;
import com.health.fragment.focusgroup.GXY_SfFragment;
import com.health.tools.GlobalConts;
import com.health.viewscontrols.medical.SfHistoryActivity;
import com.health.viewscontrols.medical.bean.SfHistoryBean;
import com.novabluetooth.views.ExamHistoryActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

/**
 * 重点人群--高血压
 * 
 * @author nova
 *
 */
public class GXYActivity extends NovaFragmentActivity implements OnClickListener {

	private ViewPager viewPager;
	private ArrayList<RadioButton> radioButtons;
	private ArrayList<Fragment> fragments;

	private TextView title;
	private Button btn_back;
	public static Button btn_up;
	FragmentAdapter adapter;
	public static Activity mGXYActivity = null;
	private LinearLayout ll_finish;
	private TextView tv_title;
	private Button bt_save;
	private Button bt_history;
	private SfHistoryBean sfHistoryBean = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		// WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.gxy_main);

		sfHistoryBean = (SfHistoryBean) getIntent().getSerializableExtra("data");
		initView();
		addListener();

	}

	private void initView() {
		title = (TextView) findViewById(R.id.title);

		btn_back = (Button) findViewById(R.id.btn_back);
		btn_up = (Button) findViewById(R.id.btn_up);
		ll_finish = (LinearLayout)findViewById(R.id.ll_finish);
		tv_title = (TextView)findViewById(R.id.tv_title);
		bt_save = (Button) findViewById(R.id.bt_save);
		bt_history = (Button) findViewById(R.id.bt_history);

		viewPager = (ViewPager) findViewById(R.id.viewpager);
		RadioButton maternity_base = (RadioButton) findViewById(R.id.sf_info_base);

		radioButtons = new ArrayList<RadioButton>();
		radioButtons.add(maternity_base);

		maternity_base.setOnClickListener(this);

		Fragment baseFragement = GXY_SfFragment.newInstance(sfHistoryBean != null ? sfHistoryBean.getId() : -1);
		fragments = new ArrayList<Fragment>();
		fragments.add(baseFragement);

		adapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
		viewPager.setAdapter(adapter);
		title.setText("高血压  —— 随访信息");
		setChecked(0);
		
		mGXYActivity = this;
	}

	private void addListener() {

		viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}

			@Override
			public void onPageSelected(int position) {
				radioButtons.get(position).setChecked(true);
				setChecked(position);
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});

		btn_back.setOnClickListener(this);
		ll_finish.setOnClickListener(this);
		bt_save.setOnClickListener(this);
		bt_history.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		int id = v.getId();
		if (id == R.id.sf_info_base) {
			viewPager.setCurrentItem(0);
			setChecked(0);
		} else if (id == R.id.ll_finish) {
			this.finish();
		} else if (id == R.id.bt_save) {
			// 保存
			onFragmentSave currentFragment = (onFragmentSave) adapter.getCurrentFragment();
			currentFragment.save();
		} else if (id == R.id.bt_history) {
			// 历史记录
			Intent intent = new Intent(this, SfHistoryActivity.class);
			intent.putExtra("type", "gxy");
			startActivityForResult(intent, ExamHistoryActivity.ResultUpdata);
		} else {
		}

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		onFragmentSave fragment = (onFragmentSave) adapter.getCurrentFragment();
		if (requestCode == GlobalConts.RequestXueya && resultCode == ExamHistoryActivity.ResultXueya) {
			fragment.setXueya(data.getStringExtra("高压"), data.getStringExtra("低压"), data.getStringExtra("脉率"));
		} else if (requestCode == GlobalConts.RequestTizhong && resultCode == ExamHistoryActivity.ResultTizhong) {
			fragment.setTizhong(data.getStringExtra("value"));
		} else if (requestCode == GlobalConts.RequestMaiLv && resultCode == com.novabluetooth.views.ExamHistoryActivity.ResultMaiLv) {
			Log.w("eeeeeeeee", data.getStringExtra("value"));
			fragment.setMaiLv(data.getStringExtra("value"));
		} else if (requestCode == ExamHistoryActivity.ResultUpdata && resultCode == Activity.RESULT_OK) {
			SfHistoryBean sfHistoryBean = (SfHistoryBean)data.getSerializableExtra("value");
			if (sfHistoryBean != null) {
//				onFragmentUpData fragment1 = (onFragmentUpData) adapter.getCurrentFragment();
//				fragment1.updateSfData(sfHistoryBean.getId());
				Intent intent = new Intent(this, GXYActivity.class);
				intent.putExtra("data", sfHistoryBean);
				startActivity(intent);
				finish();
			}
		}
	}

	@SuppressLint("ResourceAsColor")
	private void setChecked(int pos) {
		if (pos == 0) {
			tv_title.setText("高血压 — 随访信息");

		}
	}
}
